package com.heima.wemedia.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.model.admin.dtos.ChannelDto;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.wemedia.pojos.WmChannel;
import com.heima.wemedia.mapper.WmChannelMapper;
import com.heima.wemedia.service.WmChannelService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

@Service
@Transactional
@Slf4j
public class WmChannelServiceImpl extends ServiceImpl<WmChannelMapper, WmChannel> implements WmChannelService {


    @Autowired
    private WmChannelMapper wmChannelMapper;

    /**
     * 查询所有频道
     *
     * @return
     */
    @Override
    public ResponseResult channels() {
        return ResponseResult.okResult(list());
    }

    @Override
    public ResponseResult saveChanel(WmChannel wmChannel) {
        if (wmChannel == null || StringUtils.isEmpty(wmChannel.getName())) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        LambdaQueryWrapper<WmChannel> lqw = new LambdaQueryWrapper<>();
        lqw.eq(StringUtils.isNotEmpty(wmChannel.getName()), WmChannel::getName, wmChannel.getName());
        WmChannel channel = wmChannelMapper.selectOne(lqw);
        if (channel != null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST);
        }
        wmChannel.setCreatedTime(new Date());
        wmChannel.setIsDefault(true);
        int insert = wmChannelMapper.insert(wmChannel);
        if (insert == 0) {
            return ResponseResult.errorResult(AppHttpCodeEnum.SERVER_ERROR);
        }
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    final static int MAX_SIZE = 50;
    final static int DEFAULT_SIZE = 10;
    final static int DEFAULT_PAGE = 1;
    @Override
    public ResponseResult selectChannels(ChannelDto dto) {
        //入参判断
        if (dto == null) {
            return PageResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //如果入参的size和page是空或者是其他离谱的数据,需要约束一下
        Integer page = dto.getPage();
        Integer size = dto.getSize();
        if (page == 0) {
            page = DEFAULT_PAGE;
        }
        if (size == 0 || size < 10) {
            size = DEFAULT_SIZE;
        }
        Math.min(page, MAX_SIZE);

        LambdaQueryWrapper<WmChannel> lqw = new LambdaQueryWrapper<>();
        lqw.like(!org.springframework.util.StringUtils.isEmpty(dto.getName()), WmChannel::getName, dto.getName());
        //初始化分页对象
        Page<WmChannel> pageInfo = new Page<>(page, size);
        page(pageInfo, lqw);
        //返回
        ResponseResult responseResult = new PageResponseResult(page, size, (int) pageInfo.getTotal());
        responseResult.setData(pageInfo.getRecords());
        return responseResult;
    }


    @Override
    public ResponseResult deleteChannel(Integer id) {

        if (id == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        WmChannel channel = wmChannelMapper.selectById(id);
        if (channel == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        int delete = wmChannelMapper.deleteById(id);
        if (delete == 0) {
            return ResponseResult.errorResult(AppHttpCodeEnum.SERVER_ERROR);
        }
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    @Override
    public ResponseResult updateChannel(WmChannel wmChannel) {

        //入参判断
        if (wmChannel == null || StringUtils.isEmpty(wmChannel.getName())) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //查询是否有入参频道
        LambdaQueryWrapper<WmChannel> lqw = new LambdaQueryWrapper<>();
        lqw.eq(StringUtils.isNotEmpty(wmChannel.getName()), WmChannel::getName, wmChannel.getName());
        WmChannel channel = wmChannelMapper.selectOne(lqw);
        if (channel == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST);
        }
        //修改频道
//        AdChannel adChannel = new AdChannel();
//        BeanUtils.copyProperties(wmChannel, adChannel);
//        if (channel.getIsDefault()) {
//            adChannel.setIsDefault(1);
//        } else {
//            adChannel.setIsDefault(0);
//        }
//        if (channel.getStatus()) {
//            adChannel.setStatus(1);
//        } else {
//            adChannel.setStatus(0);
//        }
        int update = wmChannelMapper.updateById(wmChannel);
        if (update == 0) {
            return ResponseResult.errorResult(AppHttpCodeEnum.SERVER_ERROR);
        }
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }
}